
packages <- c("DeclareDesign","tidyverse","kableExtra","sf","magrittr","rio","stargazer","lfe",
              "car","scales","ggthemes","lubridate","survminer","survival","splitstackshape",
              "gridExtra","knitr","modelsummary","wesanderson","janitor","ggprism","sandwich",
              "lmtest","ri2","ggplot2","RColorBrewer","xtable","texreg","dplyr","tidyr","reshape2",
              "Hmisc","estimatr","patchwork","haven","scales","countrycode","schoolmath","Rmisc")

ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
ipak(packages)

rm(list = ls())
setwd(dirname(rstudioapi::getSourceEditorContext()$path))

# Import Dataset
df.final <- readRDS("wvs.rds")

regime <- c("Closed Autocracy", "Electoral Autocracy", "Electoral Democracy", "Liberal Democracy")
migrant <- c("Native", "Migrant")

#* destination by regime type, Indian ----
host.india <- ggplot(df.final[df.final$HomeCountry=="IND" & df.final$Q263==2,], aes(v2x_regime_des)) +
  geom_bar(stat = "count") +
  xlab("") +
  ylab("Num. of Immigrants")+
  scale_x_continuous(labels= regime, breaks=c(0,1,2,3))

pdf("Figures/Figure_G7A.pdf", width=6, height=4)
print(host.india)
dev.off()

#* destination by regime type, Asian ----
df.final$region <- countrycode(df.final$HomeCountry, "iso3c", "region23")

df.final.asia <- df.final %>% filter(grepl('Asia', region))

host.Asia <- ggplot(df.final.asia[df.final.asia$Q263==2,], aes(v2x_regime_des)) +
  geom_bar(stat = "count") +
  xlab("Regime Type") +
  ylab("Num. of Immigrants")+
  scale_x_continuous(labels= regime, breaks=c(0,1,2,3))

pdf("Figures/Figure_G7B.pdf", width=6, height=4)
print(host.Asia)
dev.off()
